package com.jinyang.jetpackdemo.activity.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

import com.jinyang.jetpackdemo.R;

/**
 * @Author: LiuJinYang
 * @CreateDate: 2021/12/24
 * @Description:
 */
public class MyDialogFragment extends AppCompatDialogFragment {
    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvContent2;
    private TextView tvContent3;
    private ImageView ivCancel;
    private String title;
    private String content;
    private String content2;
    private String content3;
    private View rootView;
    private int height;
    private int width;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.layout_custom_dialog, container, true);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initWindow();
        initView();
    }

    private void initView() {
       tvTitle= rootView.findViewById( R.id.tv_title );
       tvContent= rootView.findViewById( R.id.tv_content );
       tvContent2= rootView.findViewById( R.id.tv_content2 );
       tvContent3= rootView.findViewById( R.id.tv_content3 );
        ivCancel= rootView.findViewById( R.id.iv_cancel );
        tvTitle.setText(title);
        tvTitle.setVisibility(TextUtils.isEmpty(title)?View.INVISIBLE:View.VISIBLE);
        tvContent.setText(content);
        tvContent.setVisibility(TextUtils.isEmpty(content)?View.GONE:View.VISIBLE);
        tvContent2.setText(content2);
        tvContent2.setVisibility(TextUtils.isEmpty(content2)?View.GONE:View.VISIBLE);
        tvContent3.setText(content3);
        tvContent3.setVisibility(TextUtils.isEmpty(content3)?View.GONE:View.VISIBLE);
        ivCancel.setOnClickListener(v -> dismiss());
    }

    public void setTitle(String title){
       this.title=title;
    }
    public void setHeight(int height){
       this.height=height;
    }
    public void setWidth(int width){
        this.width=width;
    }
    public void setContent(String content){
       this.content=content;
    }
    public void setContent2(String content){
        this.content2=content;
    }
    public void setContent3(String content){
        this.content3=content;
    }

    private void initWindow() {
        //初始化window相关表现
        Window window = getDialog().getWindow();
        getDialog().setCancelable(false);
        //设备背景为透明（默认白色）
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置window宽高(单位px)
        window.getAttributes().width = width>0?width:1000;
        window.getAttributes().height =height>0?height: WindowManager.LayoutParams.WRAP_CONTENT;;
        //设置window位置:居中
        window.getAttributes().gravity =  Gravity.CENTER;
    }
}
